package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.bean.OrderSearch;
import com.yenlien.traceyun.business.bean.Search;
import com.yenlien.traceyun.business.entity.ClientOrder;
import com.yenlien.traceyun.business.entity.ClientOrderDetailed;
import com.yenlien.traceyun.business.entity.Invoice;
import com.yenlien.traceyun.business.entity.PayRecord;
import com.yenlien.traceyun.business.service.ClientOrderService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-12-08
 * @description:
 **/

@RestController
@RequestMapping(value = "/clientorder")
public class ClientOrderController extends BaseController {

    @Autowired
    private ClientOrderService clientOrderService;


    /**
     * 添加订单
     *
     * @param params
     * @return
     */
    @PostMapping(value = "/add")
    public Object add(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"order", "details"});
        ClientOrder order = params.getJSONObject("order").toJavaObject(ClientOrder.class);
        List<ClientOrderDetailed> details = params.getJSONArray("details").toJavaList(ClientOrderDetailed.class);
        Invoice invoice = null;
        if (params.containsKey("invoice")) {
            invoice = params.getJSONObject("invoice").toJavaObject(Invoice.class);
        }
        clientOrderService.save(order, details, invoice);
        return success(order);
    }


    @GetMapping(value = "/dele/{id}")
    public Object dele(@PathVariable("id") String id) {
        clientOrderService.del(id);
        return success("ok");
    }

    @GetMapping(value = "/load/{id}")
    public Object load(@PathVariable("id") String id) {
        return success(clientOrderService.load(id));
    }

    @PostMapping(value = "/search")
    public Object list(@RequestBody OrderSearch search) {
        return success(clientOrderService.listPage(search));
    }


    @PostMapping(value = "/client/detail")
    public Object listOrderDetail(@RequestBody OrderSearch search) {
        Page<ClientOrderDetailed> detaileds = clientOrderService.getClientOrderDetail(search);
        if (detaileds == null) {
            detaileds = new PageImpl<>(null);
        }
        return detaileds;
    }

    /**
     * 付款
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/pay")
    public Object pay(@RequestBody PayRecord record) {
        clientOrderService.pay(record);
        return success("ok");
    }

    /**
     * 获取客户订单
     *
     * @param search
     * @return
     */
    @PostMapping(value = "/client/orders")
    public Object clientOrder(@RequestBody OrderSearch search) {
        CheckParams.checkBean(search, new String[]{"relateId"});
        Page<ClientOrder> page = clientOrderService.findpage(search);
        return buildPage(page);

    }

    /**
     * 需要出库的id
     *
     * @param search
     * @return
     */
    @PostMapping(value = "/storage/orders")
    public Object needOutOrder(@RequestBody OrderSearch search) {
        CheckParams.checkBean(search, new String[]{"relateId"});
        return buildPage(clientOrderService.pageByStorageId(search));
    }
}
